/*exported JSJaCUtils */

/**
 * Various utilities put together so that they don't pollute global
 * name space.
 * @namespace
 */
export class JSJaCUtils {

  /**
   * 是否是nodeJs的运行环境
   */
  public static NODEJS_RUNTIME=(typeof window ==='undefined')?true:false

  /**
   * XOR two strings of equal length.
   * @param {string} s1 first string to XOR.
   * @param {string} s2 second string to XOR.
   * @return {string} s1 ^ s2.
   */
  static xor(s1:string, s2:string):string {
    /*jshint bitwise: false */
    if(!s1) {
      return s2;
    }
    if(!s2) {
      return s1;
    }

    var result = '';
    for(var i = 0; i < s1.length; i++) {
      result += String.fromCharCode(s1.charCodeAt(i) ^ s2.charCodeAt(i));
    }
    return result;
  }

  /**
   * Create nonce value of given size.
   * @param {int} size size of the nonce that should be generated.
   * @return {string} generated nonce.
   */
  static cnonce(size:number):string {
    let tab:string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    let cnonce:string = '';
    for (let i = 0; i < size; i++) {
      cnonce += tab.charAt(Math.round(Math.random() * (tab.length - 1)));
    }
    return cnonce;
  }

  /**
   * Current timestamp.
   * @return Seconds since 1.1.1970.
   * @type int
   */
  static now():number {
    if (Date.now && typeof Date.now == 'function') {
      return Date.now();
    } else {
      return new Date().getTime();
    }
  }
  
}
